package com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload.service;


import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.StatCustEnum;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TCargo;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TContainers;
import com.zz.ecommerce.togatherDeclareMgr.common.bean.TDocuments;
import com.zz.ecommerce.togatherDeclareMgr.custDeclarationUpload.dao.CargoCustDao;
import com.zz.ecommerce.togatherDeclareMgr.common.dao.TCargoDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <一句话功能描述>
 * <功能详细描述>
 *
 * @author Administrator
 * @date 2015/8/15,16:33
 */
@Service
public class CargoCustService {

    @Autowired
    private CargoCustDao cargoCustDao;

    @Autowired
    private TCargoDao cargoDao;

    private final String T_CONTAINERS = "T_CONTAINERS";

    private final String T_DOCUMENTS = "T_DOCUMENTS";

    @Transactional(readOnly = true)
    public String getSequence() {
        return cargoCustDao.getSequence10();
    }

    @Transactional
    public void saveCargo(TCargo cargo)
    {
        if(StringUtils.isEmpty(cargo.getIndx()))
        {
            long newSerialNo = cargoDao.queryMaxSerialNo(cargo.getConIndx()) + 1;
            if (newSerialNo > 99) {
                throw new RuntimeException("商品序号最大不能超过99");
            }
            cargo.setSerialno(String.valueOf(newSerialNo));

            cargo.setIndx(getSequence());
            cargoDao.insert(TableConstants.T_CARGO, cargo);

            return;
        }

        cargoDao.update(TableConstants.T_CARGO, cargo, " where indx=#{updateObj.indx}");
    }

    @Transactional
    public boolean deleteCargoInfo(TCargo cargo) {
        return cargoDao.deleteCargoInfo(cargo);
    }

    @Transactional(readOnly = true)
    public Map<String, String> statCargo(TCargo cargo) {

        Map<String, String> totalStatMap = new HashMap<String, String>();

        List<String> totalStatList;
        StringBuffer sb;

        for (StatCustEnum statEnum : StatCustEnum.values()) {
            totalStatList = cargoDao.statTotal(statEnum.getColName(),
                    statEnum.getColUnitName(),
                    cargo.getConIndx());
            sb = new StringBuffer();
            for (String total : totalStatList) {
                sb.append(StringUtils.isBlank(total) ? "0" : total).append(" + ");
            }

            if (sb.length() == 0) {
                totalStatMap.put(statEnum.getStatName(), "0.0");
            } else {
                totalStatMap.put(statEnum.getStatName(), sb.substring(0, sb.lastIndexOf(" + ")));
            }
        }

        return totalStatMap;
    }
    @Transactional(readOnly = true)
    public List<TContainers> queryContainerList(GridRequest gridRequest, TContainers containers) {
        return cargoCustDao.queryContainerList(T_CONTAINERS,
                containers,
                gridRequest.getStart(),
                gridRequest.getEnd(), gridRequest.getSortOrder());
    }

    @Transactional(readOnly = true)
    public long getContainerCount(TContainers containers) {
        return cargoCustDao.getContainerCount(T_CONTAINERS, containers);
    }

    @Transactional(readOnly = true)
    public TContainers queryContainerInfo(TContainers containers) {
        return cargoCustDao.queryContainerInfo(T_CONTAINERS, containers, "");
    }

    @Transactional(readOnly = false)
    public boolean saveContainerInfo(TContainers containers) {
        containers.setIndx(getSequence());
        return cargoCustDao.saveContainerInfo(containers);
    }

    @Transactional(readOnly = false)
    public boolean updateContainerInfo(TContainers containers) {
        return cargoCustDao.updateContainerInfo(containers);
    }

    @Transactional(readOnly = false)
    public boolean deleteContainerInfo(TContainers containers) {
        return cargoCustDao.deleteContainerInfo(containers);
    }

    @Transactional(readOnly = false)
    public boolean deleteTDocuments(String indx) {
        return cargoCustDao.deleteTDocuments(indx);
    }

    @Transactional(readOnly = false)
    public void saveDocuments(final TDocuments documents) {
        TDocuments qryObj = new TDocuments();
        qryObj.setIndx(documents.getIndx());
        if (null == cargoCustDao.queryOneTDocuments(T_DOCUMENTS, qryObj, "")) {
            insertDocuments(documents);
        } else {
            updateDocuments(documents);
        }
    }

    @Transactional(readOnly = false)
    private void updateDocuments(final TDocuments documents) {
        cargoCustDao.updateTDocuments(T_DOCUMENTS, documents, " where indx=#{updateObj.indx}");
    }

    @Transactional(readOnly = false)
    private void insertDocuments(final TDocuments documents) {
        cargoCustDao.inserTDocuments(T_DOCUMENTS, documents);
    }


    @Transactional(readOnly = true)
    public List<TDocuments> queryAccessoryFileList(GridRequest gridRequest, TDocuments tDocuments) {
        return cargoCustDao.queryAccessoryFileList(T_DOCUMENTS,
                tDocuments,
                gridRequest.getStart(),
                gridRequest.getEnd(), gridRequest.getSortOrder());
    }

    @Transactional(readOnly = true)
    public List<TDocuments> queryAccessoryList(GridRequest gridRequest, TDocuments tDocuments) {
        return cargoCustDao.queryAccessoryList(T_DOCUMENTS,
                tDocuments,
                gridRequest.getStart(),
                gridRequest.getEnd(), gridRequest.getSortOrder());
    }

    @Transactional(readOnly = true)
    public long getAccessoryCount(TDocuments tDocuments) {
        return cargoCustDao.getAccessoryCount(T_DOCUMENTS, tDocuments);
    }

    @Transactional(readOnly = true)
    public List<TDocuments> queryAllDocument(TDocuments documents)
    {
        return cargoCustDao.queryAllDocument(T_DOCUMENTS, documents, "");
    }
}
